/************************************************************************
This file is part of Polar4linux
Copyright (C) 2010-2011 Eric jrdn2
Portions Copyright (C) 2007 Matti Juvonen <mpj@iki.fi> (RS400 Tools)
Portions Copyright (C) 2006 Jani Hursti <jani.hursti@hut.fi>,
Portions Copyright (C) 2001-2003 Tom Oliveira e Silva

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
************************************************************************/

#include "polarExerciseBuilder.h"
#include <iostream>

polarExBuilder::polarExBuilder(polar4linuxInternals *internals0)
{
	internals=internals0;
	setupUi(this);

	changeSizePolicy(0);
	diag=new polarConnexion(internals);
	connect(diag,SIGNAL(connexionDone()),this,SLOT(getAll()));
	connect(tabWidgetExChoice,SIGNAL(currentChanged(int)),this,SLOT(changeSizePolicy(int)));
	
	QList<int> list;
	list.push_back(100);
	list.push_back(1200);
	splitter->setSizes(list);
	view->associate(internals);

	widgetBasicEx->associate(internals);
	widgetBasicEx->setPageView(view);
	
	widgetAdvEx->configure(ADVANCEDEX,internals,internals->advancedexos);
	widgetTraining->configure(TRAININGEX,internals,internals->trainingexos);
	
	widgetAdvEx->setPageView(view);
	widgetTraining->setPageView(view);
}

void polarExBuilder::changeSizePolicy(int tab)
{
	if (tab==0)
	{
		tabBasic->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
		tabAdvanced->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
		tabTraining->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
	}
	else
	{
		tabBasic->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
		tabAdvanced->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
		tabTraining->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
	}
}


polarExBuilder::~polarExBuilder()
{
	delete diag;
}

void polarExBuilder::askConnexion()
{
	diag->checkConnexion(0); //generic signal caught
}

void polarExBuilder::getAll()
{
	diag->setMaximum(3);
	diag->setProgress(0);
	diag->drawStatus(tr("Downloading basic exercises"));
	widgetBasicEx->getExos();
	diag->setProgress(1);
	diag->drawStatus(tr("Downloading advanced exercises"));
	widgetAdvEx->getExercises();
	diag->setProgress(2);
	diag->drawStatus(tr("Downloading training plan"));
	widgetTraining->getExercises();
	diag->setProgress(3);
	diag->drawStatus(tr("Done"));
	diag->hide();
	if (internals->autodisconnect==true) diag->closeIRDA();
}
